package org.rsbot.script.randoms;

import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"joku.rules"}, name="Certer", version=1.0D)
public class Certer extends Random
{
  private final int[] MODEL_IDS = { 2807, 8828, 8829, 8832, 8833, 8834, 8835, 8836, 8837 };

  private final int[] bookPiles = { 42352, 42354 };
  private final String[] ITEM_NAMES = { "bowl", "battleaxe", "fish", "shield", "helmet", "ring", "shears", "sword", "spade" };

  private boolean readyToLeave = false;
  private int failCount = 0;

  public boolean activateCondition()
  {
    return (this.game.isLoggedIn()) && (this.objects.getNearest(this.bookPiles) != null);
  }

  public int loop()
  {
    if ((!activateCondition()) && (this.readyToLeave)) {
      this.readyToLeave = false;
      this.failCount = 0;
      log("I think we've solved the certer");
      return -1;
    }

    if (this.interfaces.getComponent(241, 4).containsText("Ahem, ")) {
      this.readyToLeave = false;
    }

    if ((this.interfaces.getComponent(241, 4).containsText("Correct.")) || (this.interfaces.getComponent(241, 4).containsText("You can go now.")))
    {
      this.readyToLeave = true;
    }
    int i;
    Object localObject;
    if (this.readyToLeave) {
      i = 11368;
      localObject = this.objects.getNearest(new int[] { i });
      if (localObject != null) {
        RSTile localRSTile = ((RSObject)localObject).getLocation();
        if (this.calc.distanceTo((RSObject)localObject) < 4) {
          ((RSObject)localObject).doAction("Enter");
          return random(3000, 4000);
        }
        this.walking.walkTileMM(this.walking.randomize(new RSTile(localRSTile.getX() - 1, localRSTile.getY()), 1, 1));

        return random(6000, 8000);
      }

    }

    if (this.interfaces.getComponent(184, 0).isValid()) {
      i = this.interfaces.getComponent(184, 8).getComponents()[3].getModelID();

      localObject = null;
      for (int j = 0; j < this.MODEL_IDS.length; j++) {
        if (this.MODEL_IDS[j] == i) {
          localObject = this.ITEM_NAMES[j];
        }
      }

      if (localObject == null) {
        log("The object couldn't be identified! ID: " + i);
        this.failCount += 1;
        if (this.failCount > 10) {
          stopScript(false);
          return -1;
        }
        return random(1000, 2000);
      }

      for (j = 0; j < 3; j++) {
        RSComponent localRSComponent = this.interfaces.getComponent(184, 8).getComponents()[j];

        if (localRSComponent.containsText((String)localObject)) {
          localRSComponent.doClick();
          return random(3000, 5000);
        }
      }
    }

    if (this.interfaces.canContinue()) {
      this.interfaces.clickContinue();
      return random(3000, 4000);
    }

    RSNPC localRSNPC = this.npcs.getNearest(new String[] { "Niles", "Miles", "Giles" });
    if (localRSNPC != null) {
      if (this.calc.distanceTo(localRSNPC) < 4) {
        localRSNPC.doAction("Talk-to");
        return random(4000, 5000);
      }
      localObject = localRSNPC.getLocation();
      this.walking.walkTileMM(this.walking.randomize(new RSTile(((RSTile)localObject).getX() + 2, ((RSTile)localObject).getY()), 1, 1));

      return random(6000, 8000);
    }

    this.failCount += 1;
    if (this.failCount > 10) {
      stopScript(false);
      return -1;
    }
    return random(1000, 2000);
  }
}